<?php

/**
 * @Author: kendy
 * @Date:   2020-01-15 15:42:05
 * @Last Modified by:   jiayanting
 * @Last Modified time: 2020-03-31 09:14:47
 */
namespace app\api\controller;
use think\Db;
use think\Request;
use think\Controller;
use think\Loader;
use app\api\model\Payments as p;
class Alipays extends Nocommon
{
    public function getAlipays()
    {
        Loader::import('Alipay.aop.AopClient');
        $aop = new \AopClient();

        $config = config('alipay');
        $aop->gatewayUrl = $config['gatewayUrl'];
        $aop->appId = $config['appId'];
        $aop->rsaPrivateKey = $config['rsaPrivateKey'];
        $aop->format = $config['format'];
        $aop->postCharset = $config['postCharset'];
        $aop->signType = $config['signType'];
        $aop->alipayrsaPublicKey = $config['alipayrsaPublicKey'];

        Loader::import('Alipay.aop.request.AlipayTradeAppPayRequest');

        $request = new \AlipayTradeAppPayRequest();
        $order_sn = input("order_sn"); 
        $order_type = input("order_type");//订单类型 订单类型 1酒店，2景点，3跟团 
        $orderTable = $this->getFieldByType($order_type); 
        $cnt = Db::name($orderTable[0])
		->where(['order_sn'=>$order_sn])
		->find(); 
        if(empty($cnt)) 
        	 $this->apiReturn('400','订单信息为空',0); 
        $order_price = $cnt["order_price"];
        $uid = $cnt[$orderTable[1]];
        $passback_params = $uid."@".$order_type;
        $bizcontentArr= array(
            'subject'=>'支付购买商品费用'.$order_price.'元',
            'out_trade_no'=>$order_sn,
            'total_amount'=>$order_price,
            'passback_params'=>UrlEncode($passback_params),
            'product_code'=>'QUICK_MSECURITY_PAY'
        ); 
        $request->setNotifyUrl('http://api.lancly.com/api/Alipays/aliNotify');
        $bizcontent = json_encode($bizcontentArr);
        $request->setBizContent($bizcontent);
        $response['payments_url'] = $aop->sdkExecute($request);
        $response['order_id'] = $cnt['order_id'];   
        return $this->apiReturn('200','请求成功',$response); 

    }

     public function aliNotify(){	 
     	 $p = new p();
     	 if ($_POST['trade_status'] == 'TRADE_SUCCESS') {//如果支付成功
            file_put_contents("obj2.txt", json_encode($_POST));
             //商户订单号
			$obj = array();
			$obj["trade_no"] = $_POST['trade_no'];
			$obj["out_trade_no"] = $_POST['out_trade_no'];
			$obj["total_amount"] = $_POST['total_amount'];
			$extras = explode("@",urldecode($_POST['passback_params']));
			$obj["uid"] = $extras[0];
			$obj["order_type"] = $extras[1];
			$obj["pay_type"] = 1;
			//支付成功业务逻辑
            if(in_array($obj['order_type'], [1,2,3])){ 
              $rs = $p->complatePay($obj);
            }
             if($obj['order_type'] == 4){ 
              $rs = $p->complateJlouPay($obj);
            }
             if($obj['order_type'] == 5){ 
              $rs = $p->complateBargainPay($obj);
            }
             if($obj['order_type'] == 6){
                 $rs = $p->complateVotePay($obj);
             }
			if($rs["status"]==200){
				echo 'success';
			}else{
				echo 'fail';
			}
     	 }else{
     	 	echo 'fail';
     	 }

     }


     /**
	 * [getFieldByType 根据类型获取字段]
	 * @Author   kendy
	 * @DateTime 2020-02-11T13:01:53+0800
	 * @param    [type]                   $payType [支付类型 1酒店，2景点，3跟团]
	 * @return   [type]                            []
	 */
	public function getFieldByType($orderType){  
	      if($orderType==1){
	      	 return  ['hotel_order','uid','hotel','hotel_id','hotel_id','hotel_name']; 
	      }
	      if($orderType==2){
	         return  ['tour_order','user_id','tours','tour_id','tour_id','tour_name']; 
	      }
	      if($orderType==3){ 
	        return   ['gtour_order','user_id','group_tour','id','gtour_id','gtour_title']; 
	      } 
	} 


}